package com.le.tester.webpage.WXPay.controller;

import com.le.tester.webpage.WXPay.dto.WxPayDto;
import com.le.tester.webpage.WXPay.entiry.WxPay;
import com.le.tester.webpage.WXPay.service.PayInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：微信支付controller
 */
@Controller
@RequestMapping("pay/")
public class WxPayController {
    private static final Logger LOG = LoggerFactory.getLogger(WxPayController.class);

    @Autowired
    private PayInfoService payInfoService;

    @ResponseBody
    @RequestMapping(value = "wxPay")
    public Map<String, Object> wxPay(@RequestBody WxPayDto wxPayDto) {

        Map<String, Object> resultMap = new HashMap<>();
        //进行参数校验
        if (StringUtils.isBlank(wxPayDto.getOpenId()) || StringUtils.isBlank(wxPayDto.getOwnOrderNo())) {
            resultMap.put("code", 500);
            resultMap.put("msg", "请求参数异常");
            resultMap.put("resultData", null);
            return resultMap;
        }

        WxPay wxPay = payInfoService.getPayInfoByOrderNo(wxPayDto.getOwnOrderNo());
        if (null != wxPay) {
            //是分开的，get和save不要写在一起
            payInfoService.saveWxPayInfo(wxPay);
        }
        try {
            resultMap = payInfoService.deployPay();
            return resultMap;
        } catch (Exception e) {
            LOG.info("调用微信支付出现异常:" + e);
            resultMap.put("code", 500);
            resultMap.put("msg", "异常");
            resultMap.put("resultData", null);
            return resultMap;
        }
    }

    @ResponseBody
    @RequestMapping(value = "wxPayNotify")
    public void wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            String resXml = payInfoService.wxPayNotify();
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            out.write(resXml.getBytes());
            out.flush();
            out.close();
        } catch (Exception e) {
            LOG.info("微信支付回调异常，{}", e.getMessage());
        }

    }

    //优化的微信支付
    @ResponseBody
    @RequestMapping(value = "wxPayBetter")
    public Map<String, Object> wxPayBetter(@RequestBody WxPayDto wxPayDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap = payInfoService.deployWxPay(wxPayDto);
            return resultMap;
        } catch (Exception e) {
            LOG.info("微信支付预下单异常:" + e);
            resultMap.put("code", 500);
            resultMap.put("msg", "error");
            return resultMap;
        }

    }

    //优化的微信支付回调
    @ResponseBody
    @RequestMapping(value = "wxPayNotifyBetter")
    public void wxPayNotifyBetter(HttpServletResponse response) {
        try {
            WxPayDto wxPayDto = new WxPayDto();
            String resXml = payInfoService.wxPayNotifyBetter(wxPayDto);
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            out.write(resXml.getBytes(StandardCharsets.UTF_8));
            out.flush();
            out.close();
        } catch (Exception e) {
            LOG.info("微信支付回调异常:" + e);
        }

    }
}
